package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.excel.annotation.RequestExcel;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.VO.AirFactorExcelVO;
import com.iwomy.envpro.platform.entity.AirFactorEntity;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.entity.DataTemplateDetailsEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.AirFactorService;
import com.iwomy.envpro.platform.service.DataTemplateDetailsService;
import com.iwomy.envpro.platform.service.DataTemplateService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 监测因子
 *
 * @author pig
 * @date 2023-08-25 14:56:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/airFactor")
@Tag(description = "airFactor", name = "监测因子管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AirFactorController {

	private final AirFactorService airFactorService;

	private final DataTemplateDetailsService dataTemplateDetailsService;

	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param airFactor 监测因子
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_airFactor_view')")
	public R getAirFactorPage(@ParameterObject Page page, @ParameterObject AirFactorEntity airFactor) {
		LambdaQueryWrapper<AirFactorEntity> wrapper = Wrappers.lambdaQuery(airFactor);
		return R.ok(airFactorService.page(page, wrapper));
	}


	/**
	 * 通过id查询监测因子
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_airFactor_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(airFactorService.getById(id));
	}

	/**
	 * 新增监测因子
	 *
	 * @param airFactor 监测因子
	 * @return R
	 */
	@Operation(summary = "新增监测因子", description = "新增监测因子")
	@SysLog("新增监测因子")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_airFactor_add')")
	public R save(@RequestBody AirFactorEntity airFactor) {

		AirFactorEntity airFactorEntity = new AirFactorEntity();
		airFactorEntity.setCnName(airFactor.getCnName());
		LambdaQueryWrapper<AirFactorEntity> airFactorEntityLambdaQueryWrapper = Wrappers.lambdaQuery(airFactorEntity);
		AirFactorEntity one = airFactorService.getOne(airFactorEntityLambdaQueryWrapper);
		if (ObjectUtil.isNotNull(one))
			throw new PlatformException("中文名已存在");

		return R.ok(airFactorService.save(airFactor));
	}

	/**
	 * 修改监测因子
	 *
	 * @param airFactor 监测因子
	 * @return R
	 */
	@Operation(summary = "修改监测因子", description = "修改监测因子")
	@SysLog("修改监测因子")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_airFactor_edit')")
	public R updateById(@RequestBody AirFactorEntity airFactor) {
		long count = dataTemplateDetailsService.count(Wrappers.<DataTemplateDetailsEntity>lambdaQuery().eq(DataTemplateDetailsEntity::getFactorId, airFactor.getId()));

		if (count > 0)
			return R.failed("监测因子已关联数据模板，不允许编辑");
		return R.ok(airFactorService.updateFactor(airFactor));
	}

	/**
	 * 通过id删除监测因子
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除监测因子", description = "通过id删除监测因子")
	@SysLog("通过id删除监测因子")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_airFactor_del')")
	public R removeById(@RequestBody Long[] ids) {

		long count = dataTemplateDetailsService.count(Wrappers.<DataTemplateDetailsEntity>lambdaQuery().in(DataTemplateDetailsEntity::getFactorId, ids));

		if (count > 0)
			return R.failed("监测因子已关联数据模板，不允许删除");

		return R.ok(airFactorService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param airFactor 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_airFactor_export')")
	public List<AirFactorEntity> export(AirFactorEntity airFactor) {
		return airFactorService.list(Wrappers.query(airFactor));
	}

	@PostMapping("/import")
	public R importFactor(@RequestExcel List<AirFactorExcelVO> excelVOList, BindingResult bindingResult) {
		return airFactorService.importFactor(excelVOList, bindingResult);
	}


	/**
	 * 导出excel 表格
	 *
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/template")
	@PreAuthorize("@pms.hasPermission('platform_airFactor_export')")
	public List<AirFactorExcelVO> template() {

		List<AirFactorExcelVO> list = new ArrayList<>();
		AirFactorExcelVO airFactorVO = new AirFactorExcelVO();
		list.add(airFactorVO);
		return list;
	}


	/**
	 * list
	 */
	@GetMapping("/list")
	public R list(AirFactorEntity airFactor) {
		return R.ok(airFactorService.list(Wrappers.query(airFactor)));
	}
}